package de.syfy.project.engine;

import static org.lwjgl.opengl.GL11.*;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Timo
 * 
 * Tile wird genutzt um Informationen über einen Abschnitt der Karte zu liefern.
 */
public class Tile {
    private static List<Tile> TILE_LIST = new ArrayList<Tile>();
    public static int TILE_WIDTH = 32;
    public static int TILE_HEIGHT = 32;
    private Color c; 
    private Texture texture;
    private int unique_id = -1;
    private boolean isWalkable = true;
    
    public Tile(TextureLoader loader, String path){
        this();
        try {
            texture = loader.getTexture(path);
        } catch (IOException ex) {
            //Später ein Logger
        }
    }
    
    public Tile(Color c){
        this();
        this.c = c;
    }
    
    private Tile(){
        TILE_LIST.add(this);
        unique_id = TILE_LIST.indexOf(this);
    }
    
    public int getID(){
        return unique_id;
    }
    
    public boolean usesTexture(){
        return texture != null;
    }
    /*
     * Zeichnet das Tile an dem gegebenen Koordinaten!
     */
    public void drawTile(float x,float y){   
        if(texture != null){
            glTranslatef(0,0,0.0f);
            glEnable(GL_TEXTURE_2D);
            glPushMatrix();
            texture.bind();
            glTranslatef(x, y, 0);
            glBegin(GL_QUADS);
                glTexCoord2f(0, 0);
                glVertex2f(0, 0);
                glTexCoord2f(0, texture.getHeight());
                glVertex2f(0, TILE_HEIGHT);
                glTexCoord2f(texture.getWidth(), texture.getHeight());
                glVertex2f(TILE_WIDTH, TILE_HEIGHT);
                glTexCoord2f(texture.getWidth(), 0);
                glVertex2f(TILE_WIDTH, 0);
            glEnd();
            glPopMatrix();
            glDisable(GL_TEXTURE_2D);
        }else{
            glDisable(GL_TEXTURE_2D);
            glBegin(GL_QUADS);
                glColor3f(c.getRed(),c.getGreen(),c.getBlue());
                glVertex2f(x,y);
                glVertex2f(x+TILE_WIDTH,y);
                glVertex2f(x+TILE_WIDTH,y+TILE_HEIGHT);
                glVertex2f(x,y+TILE_HEIGHT);
            glEnd();
        }
    }
    
    public static void drawBlack(float x, float y){
        glDisable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
            glColor3f(0f,0f,0f);
            glVertex2f(x,y);
            glVertex2f(x+TILE_WIDTH,y);
            glVertex2f(x+TILE_WIDTH,y+TILE_HEIGHT);
            glVertex2f(x,y+TILE_HEIGHT);
        glEnd();
    }
    
    public static Tile getTileWithID(int id){
        return TILE_LIST.get(id);
    }
}
